package com.fuelstations.coderone;

import com.example.androidhive.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Maps extends Activity {

	
	private static GoogleMap mMap;

	private static LatLng myLocation;

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maps);
		_getLocation();
		mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

		Intent intent = getIntent();

		double lat = Double.parseDouble(intent.getStringExtra("lat"));
		double lng = Double.parseDouble(intent.getStringExtra("lng"));
		String addr = intent.getStringExtra("addr");

		final LatLng stationPos = new LatLng(lat, lng);

		mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(stationPos, 15.0f));

		mMap.addMarker(new MarkerOptions().position(stationPos).title(addr).snippet("info"));

		mMap.addMarker(new MarkerOptions().position(myLocation).title(addr).snippet("info").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

		
		
		Button button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String urlNavi = "http://maps.google.com/maps?saddr=" + Double.toString(myLocation.longitude) + "," + Double.toString(myLocation.latitude) + "&daddr=" + Double.toString(stationPos.longitude)
						+ "," + Double.toString(stationPos.latitude);
				Intent navigation = new Intent(Intent.ACTION_VIEW, Uri.parse(urlNavi));
				startActivity(navigation);
			}
		});
		

	}

	private void _getLocation() {
		// Get the location manager
		LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		String bestProvider = locationManager.getBestProvider(criteria, false);
		Location location = locationManager.getLastKnownLocation(bestProvider);

		LocationListener loc_listener = new LocationListener() {
			public void onLocationChanged(Location l) {
			}

			public void onProviderEnabled(String p) {
			}

			public void onProviderDisabled(String p) {
			}

			public void onStatusChanged(String p, int status, Bundle extras) {
			}
		};
		locationManager.requestLocationUpdates(bestProvider, 0, 0, loc_listener);
		location = locationManager.getLastKnownLocation(bestProvider);

		try {

			myLocation = new LatLng(location.getLatitude(), location.getLongitude());
		} catch (NullPointerException e) {
			myLocation = new LatLng(-1.0, -1.0);
		}
	}
}